// detail模块的小仓库
import { reqGoodsInfo, reqAddOrUpdateToCart } from "@/api"
import { getUUID } from '@/utils/uuid_token'

const state = {
  goodsInfo: {},
  tocartInfo: [],
  // 游客临时身份
  uuid_token: getUUID()
}
const mutations = {
  GETGOODSINFO(state, goodsInfo) {
    state.goodsInfo = goodsInfo
  },
  ADDORUPDATETOCART(state, tocartInfo) {
    state.tocartInfo = tocartInfo
  }
}
const actions = {
  // 获取商品信息
  async getGoodsInfo({ commit }, skuId) {
    let { data: res } = await reqGoodsInfo(skuId)
    if (res.code == 200) {
      commit('GETGOODSINFO', res.data)
    }
  },
  // 添加 || 修改 商品信息到购物车
  async addOrUpdateToCart({ commit }, { skuId, skuNum }) {
    let { data: res } = await reqAddOrUpdateToCart(skuId, skuNum)
    if (res.code == 200) {
      commit('ADDORUPDATETOCART', res.data)
    }
  }
}

// 简化数据而生
const getters = {
  categoryView(state) {
    return state.goodsInfo.categoryView || {}
  },
  skuInfo(state) {
    return state.goodsInfo.skuInfo || {}
  },
  spuSaleAttrList(state) {
    return state.goodsInfo.spuSaleAttrList || []
  }
}


export default {
  state,
  mutations,
  actions,
  getters
}